package com.paradisecloudtech.vc.system.ui.activity;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paradisecloudtech.vc.system.R;
import com.paradisecloudtech.vc.system.entity.Constant;
import com.common.library.base.BaseActivity;
import com.paradisecloudtech.vc.system.mvp.bean.LoginResultBean;
import com.paradisecloudtech.vc.system.mvp.presenter.LoginPresenter;
import com.paradisecloudtech.vc.system.mvp.view.LoginView;
import com.common.library.Utils.ActivityUtil;
import com.common.library.Utils.SpUtil;
import com.common.library.Utils.ToastUtil;
import com.common.library.Utils.UIUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements LoginView {

    private LoginPresenter mLoginPresenter;

    @BindView(R.id.et_login_name)
    public EditText etLoginName;

    @BindView(R.id.et_login_password)
    public EditText etLoginPassword;

    @BindView(R.id.tv_login_btn)
    public TextView tvLoginBtn;

    @BindView(R.id.iv_login_loading)
    public ImageView ivLoginLoading;

    @BindView(R.id.rl_login_root)
    public RelativeLayout rlLoginRoot;

    @BindView(R.id.rl_login_content)
    public RelativeLayout rlLoginContent;

    @BindView(R.id.iv_login_logo)
    public ImageView ivLoginLogo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    public void intVar() {
        mLoginPresenter = new LoginPresenter();
    }

    @Override
    public void initData() {

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) rlLoginRoot.getLayoutParams();
        params.topMargin = UIUtil.getStatusBarHeight();
        rlLoginRoot.setLayoutParams(params);

    }

    @Override
    public void initListener() {
        mLoginPresenter.attachView(this);
        rlLoginContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (rlLoginContent.getViewTreeObserver().isAlive()) {
                    rlLoginContent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                int center = (ivLoginLogo.getTop() + rlLoginContent.getBottom()) / 2;
                int dy = center - UIUtil.getDisplayHeight() / 2;
                rlLoginContent.setVisibility(View.VISIBLE);
                ObjectAnimator objectTranslationY = ObjectAnimator.ofFloat(ivLoginLogo, "translationY", ivLoginLogo.getTranslationY(), ivLoginLogo.getTranslationY() - dy);
                objectTranslationY.setDuration(500);
                objectTranslationY.setStartDelay(0);
                objectTranslationY.setInterpolator(new LinearInterpolator());
                objectTranslationY.start();

                ObjectAnimator translationY = ObjectAnimator.ofFloat(rlLoginContent, "translationY", rlLoginContent.getTranslationY(), rlLoginContent.getTranslationY() - dy);// AnimatorSet animatorSet = new AnimatorSet();
                translationY.setDuration(500);
                translationY.setStartDelay(0);
                translationY.setInterpolator(new LinearInterpolator());
                translationY.start();
            }
        });
    }


    @OnClick({R.id.tv_login_btn})
    public void onClickView(View v) {
        switch (v.getId()) {
            case R.id.tv_login_btn:
                String name = etLoginName.getText().toString();
                String password = etLoginPassword.getText().toString();
                mLoginPresenter.requestLoginData(name, password);

//                LogUtil.e("decorView****" + getWindow().getDecorView().getHeight());
//                LogUtil.e("dip****" + UIUtil.getDisplayHeight());
//                LogUtil.e("stateBar****" + UIUtil.getStatusBarHeight());
//                LogUtil.e("TitleBar****" + UIUtil.getTitleBarHeight(this));
//                LogUtil.e("ActionBar****" + UIUtil.getActionBarHeight(this));
//                LogUtil.e("NavigationBarHeight****" + UIUtil.getNavigationBarHeight());
//                LogUtil.e("rootView****" + UIUtil.getRootView(this));
//                LogUtil.e("contentView****" + UIUtil.getContentViewHeight(this));
//                LogUtil.e("height****" + UIUtil.dp2px(25));

                break;
        }
    }

    @Override
    public void startLogin() {
        ivLoginLoading.setVisibility(View.VISIBLE);
        Animation loginLoadingAnimation = AnimationUtils.loadAnimation(mContext, R.anim.login_activity_loginloading);
        ivLoginLoading.setAnimation(loginLoadingAnimation);
    }

    @Override
    public void loginSuccess(LoginResultBean loginResult) {
        if (loginResult.getErrorCode() == 0) {
            ToastUtil.showToast(R.string.login_success);
            SpUtil.putLong(Constant.TIME, System.currentTimeMillis());
            SpUtil.putString(Constant.USERID, loginResult.getResult().getUser().getUserid());
            ActivityUtil.startActivity(mContext, MainActivity.class);
            finish();
        } else if (loginResult.getErrorCode() == 3) {
            ToastUtil.showToast(R.string.password_or_account_error);
        } else {
            ToastUtil.showToast(R.string.login_failed);
        }
    }

    @Override
    public void endLogin() {
        ivLoginLoading.clearAnimation();
        ivLoginLoading.setVisibility(View.INVISIBLE);
    }

    @Override
    public void requestFailedByNet(Throwable throwable) {
        ivLoginLoading.clearAnimation();
        ivLoginLoading.setVisibility(View.INVISIBLE);
        ToastUtil.showToast(throwable.getMessage());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLoginPresenter.detachView();
    }

}
